﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Common.Controls
{
    public partial class RoleContent : ContentFormControlBase
    {
        protected override List<Control> ControlSet
        {
            get
            {
                List<Control> ctrls = new List<Control>();
                ctrls.Add(this.ddlRoleType);
                ctrls.Add(this.txtRoelName);
                ctrls.Add(this.btnSave);

                return ctrls;
            }
        }

        #region 界面事件
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Response.Expires = -1;
            this.GetParams();

            if (!IsPostBack)
            {
                if (this._Action == ActionType.New)
                {
                    this.InitNewForm();
                }
                else
                {
                    this.InitFormData();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!this.Page.IsValid)
            {
                return;
            }

            try
            {
                Role role = null;
                if (this._Action == ActionType.New)
                {
                    role = new Role();
                }
                else
                {
                    role = UserHelper.GetRoleByID(this.hidRoleID.Value);
                }

                this.GetFormData(role);
                role.Save();
                this.hidRoleID.Value = role.ID.ToString();
                this.InitFormData();

                string returnVal = string.Format("Call:ExpendNode('{0}');#;", role.ID.ToString().ToUpper());
                ClientHelper.Instance.CloseWindow("保存成功！", returnVal, RefreshType.NoRefresh);
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }
        #endregion

        #region 成员方法
        /// <summary>
        /// 获取参数
        /// </summary>
        private void GetParams()
        {
            if (string.IsNullOrEmpty(Request["Action"]))
            {
                throw new ArgumentNullException("Acion", "打开表单时，缺少操作参数");
            }
            this._Action = (ActionType)Enum.Parse(typeof(ActionType), Request["Action"], true);

            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                this.hidRoleID.Value = Request["ID"].ToString();
            }
        }
        private void InitNewForm()
        {
        }
        /// <summary>
        /// 初始化表单数据
        /// </summary>
        private void InitFormData()
        {
            string roleID = this.hidRoleID.Value;
            Role role = UserHelper.GetRoleByID(roleID);

            if (role != null)
            {
                string roleType = role.Type.ToString();
                ListItem item = this.ddlRoleType.Items.FindByValue(roleType);
                if (item != null)
                {
                    item.Selected = true;
                }

                this.txtRoelName.Text = role.Name;
            }
        }
        private void GetFormData(Role role)
        {
            role.Type = (RoleTypes)Enum.Parse(typeof(RoleTypes), this.ddlRoleType.SelectedValue);
            role.Name = this.txtRoelName.Text.Trim();
        }
        #endregion
    }
}